package com.suntimes.guangjiegou.fragment;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.suntimes.cl.util.CLLog;
import com.suntimes.guangjiegou.R;

@SuppressLint("ValidFragment")
public class NavigationFragment extends BaseFragment {
	
	public static final String TAG = "NavigationFragment";
	
	protected List<NavigationSubFragment> fragmentList = new ArrayList<NavigationSubFragment>();
	private ViewGroup mSubFragment;
	
	
	public NavigationFragment(NavigationSubFragment rootFragment){
		rootFragment.setNavigationFragment(this);
		rootFragment.setRootFragment(true);
		fragmentList.add(rootFragment);
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = super.onCreateView(inflater, container, savedInstanceState);
		
		FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        NavigationSubFragment rootFragment = fragmentList.get(0);
        transaction.add(this.hashCode(), rootFragment, getFragmentKey(rootFragment));
        transaction.commit();
		return view;
	}
	
	protected View loadView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState){
		View view = inflater.inflate(R.layout.page_navigation, null, false);
		view.setBackgroundColor(Color.BLACK);
		mSubFragment = (ViewGroup)view.findViewById(R.id.page_navigation_container);
		mSubFragment.setId(this.hashCode());
		
		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		CLLog.i(TAG, "onResume");
		
//		FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
//		Fragment currentFragment = fragmentManager.findFragmentById(this.hashCode());
//		
//		if(currentFragment!=null){
//			FragmentTransaction transaction = fragmentManager.beginTransaction();
//			transaction.show(currentFragment);
//			transaction.commit();
//		}
		
	}
	

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@Override
	public void onPause() {
		super.onPause();
		
		CLLog.i(TAG, "onPause ");
//		FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
//		Fragment currentFragment = fragmentManager.findFragmentById(this.hashCode());
//		
//		if(currentFragment!=null){
//			FragmentTransaction transaction = fragmentManager.beginTransaction();
//			transaction.hide(currentFragment);
//			transaction.commit();
//		}
	}

	
	public void push(NavigationSubFragment fragment){
		this.push(fragment, true);
	}

	public void push(NavigationSubFragment fragment, boolean animation){
		CLLog.i(TAG, "push");
		fragment.setNavigationFragment(this);
		fragmentList.add(fragment);
		
		FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if(animation){
        	transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.anim_zoom_in);
        }
        Fragment currentFragment = fragmentManager.findFragmentById(this.hashCode());
        if(currentFragment!=null){
//        	transaction.detach(currentFragment);
        	transaction.hide(currentFragment);
        	currentFragment.onPause();
        	currentFragment.onStop();
        }
        
        transaction.add(this.hashCode(), fragment, getFragmentKey(fragment));
        transaction.commit();
        
	}
	
	public void pop(){
		CLLog.i(TAG, "pop");
		if(fragmentList.size()>1){
			NavigationSubFragment fragment = fragmentList.get(fragmentList.size()-2);
			popToFragment(fragment);
		}
	}
	
	public void popToRootFragment(){
		popToRootFragment(true);
	}
	
	public void popToRootFragment(boolean showAnimation){
		if(fragmentList.size()> 1){
			NavigationSubFragment fragment = fragmentList.get(0);
			popToFragment(fragment,showAnimation);
		}
	}
	
	public BaseFragment getRootFragment(){
		BaseFragment fragment = null;
		if(fragmentList.size()>0){
			fragment  = fragmentList.get(0);
		}
		return fragment;
	}
	
	
	public void popToFragment(NavigationSubFragment fragment){
		popToFragment(fragment,true);
	}  
	
	
	public void popToFragment(NavigationSubFragment fragment, boolean animation){
		FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if(animation){
        	 transaction.setCustomAnimations(R.anim.anim_zoom_out, R.anim.slide_out_right);
        }
       
        Fragment currentFragment = fragmentManager.findFragmentById(this.hashCode());
        
        if(currentFragment!=null){
        	transaction.detach(currentFragment);
//        	transaction.show(currentFragment);
        }
        
        transaction.show(fragment);
        fragment.onStart();
        fragment.onResume();
        
        List<BaseFragment> removeList = new ArrayList<BaseFragment>();
        int index = fragmentList.indexOf(fragment);
        if(index>=0){
        	for(int i=index+1;i<fragmentList.size();i++){
        		BaseFragment removeFragment = fragmentList.get(i);
        		removeFragment.setNavigationFragment(null);
        		removeList.add(removeFragment);
        		transaction.remove(removeFragment);
        	}
        	fragmentList.removeAll(removeList);
        }
//        transaction.replace(this.hashCode(), fragment, getFragmentKey(fragment));
        transaction.commit();
		
	}
	
	private String getFragmentKey(BaseFragment fragment){
//		return fragment.hashCode() + "";
		return fragment.getClass().getName();
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			boolean isSubFragmentHandle = false;
			
			FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
			BaseFragment currentFragment = (BaseFragment)fragmentManager.findFragmentById(this.hashCode());
			if(currentFragment!=null){
				isSubFragmentHandle = currentFragment.onKeyDown(keyCode, event);
			}
			if(!isSubFragmentHandle){
				if(fragmentList.size()>1){
					this.pop();
					return true;
				}else{
					return false;
				}
			}else{
				return true;
			}
			
		}
		return false;
	}
	
}
